package cn.com.xuxiaowei.aspect;

import cn.com.xuxiaowei.annotation.DataSource;
import cn.com.xuxiaowei.config.DataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author xuxiaowei
 */
@Slf4j
@Aspect
@Component
public class DataSourceAspect {

	@Pointcut("@within(cn.com.xuxiaowei.annotation.DataSource) || @annotation(cn.com.xuxiaowei.annotation.DataSource)")
	public void pointCut() {

	}

	@Before("pointCut() && @annotation(dataSource)")
	public void doBefore(DataSource dataSource) {
		log.info("选择数据源：{}", dataSource.value());
		DataSourceContextHolder.setDataSource(dataSource.value());
	}

	@After("pointCut() && @annotation(dataSource)")
	public void doAfter(DataSource dataSource) {
		log.info("清理数据源：{}", dataSource.value());
		DataSourceContextHolder.clear();
	}

}
